package com.aishua.fire.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.entity.app.APPExerciseBasicReqInfo;
import com.aishua.fire.entity.app.APPExerciseBasicReqInfo_wcc;
import com.aishua.fire.entity.app.APPExerciseConclusionReqInfo;
import com.aishua.fire.entity.app.APPExerciseDataRespInfo;
import com.aishua.fire.entity.app.APPExerciseDelayReqInfo;
import com.aishua.fire.entity.app.Picture;
import com.aishua.fire.entity.drillrecord.DrillRecordCatalogInfo_App;
import com.aishua.fire.entity.drillrecord.DrillRecordExampleInfo;
import com.aishua.fire.entity.drillrecord.DrillRecordExampleInfo_wcc;
import com.aishua.fire.entity.emergencyPre.ExercisePlanDataRespInfo;
import com.aishua.fire.entity.preExercise.DepartmentRespInfo;
import com.aishua.fire.entity.preExercise.ExercisePlanInfo;
import com.aishua.fire.entity.preExercise.ExercisePlanReqInfo;
import com.aishua.fire.entity.preExercise.ExerciseStudyPlanInfo;
import com.aishua.fire.entity.preExercise.ExerciseStudyPlanReqInfo;
import com.aishua.fire.service.PreExerciseManagerService;
import com.github.pagehelper.PageHelper;
import com.aishua.fire.mapper.PreExerciseMapper;

@Service
public class PreExerciseManagerServiceImpl implements PreExerciseManagerService {

	@Autowired
	private PreExerciseMapper preExerciseMapper;

	@Override
	public List<ExercisePlanInfo> searchPreExerciseList(String plan_name, String plan_types, String time,
			int pageNumber, int pageSize,String state) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("plan_name", plan_name);
		pa.put("plan_types", plan_types);
		pa.put("time", time);
		pa.put("state", state);
		List<ExercisePlanInfo> list = preExerciseMapper.searchPreExerciseList(pa);
		return list;
	}


	@Override
	public void delPreExercise(String id) {
		preExerciseMapper.delPreExercise(id);

	}

	@Override
	public void addPreExercise(ExercisePlanReqInfo planInfo) {
		preExerciseMapper.addPreExercise(planInfo);
		
	}

	@Override
	public void updatePreExercise(ExercisePlanReqInfo planInfo) {
		preExerciseMapper.updatePreExercise(planInfo);		
	}

	@Override
	public List<DepartmentRespInfo> getdepartmentInfo() {
		return preExerciseMapper.getdepartmentInfo();
	}

	@Override
	public void addStudyPlan(ExerciseStudyPlanReqInfo studyInfo) {
		preExerciseMapper.addStudyPlan(studyInfo);
		if(studyInfo.getPlan_department_id()!=null) {
			for(String plan_department_id :studyInfo.getPlan_department_id()) {
				Map<String, String> ma = new HashMap<String, String>();
				ma.put("plan_department_id", plan_department_id);
				ma.put("study_id", studyInfo.getId());
				preExerciseMapper.addStudyPlanWithDepartment(ma);
			}
		}
	}

	@Override
	public List<ExerciseStudyPlanInfo> searchStudyPlanList(String plan_name, String plan_types, String study_time,
			String prepare_name, int pageNumber, int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("plan_name", plan_name);
		pa.put("plan_types", plan_types);
		pa.put("study_time", study_time);
		pa.put("prepare_name", prepare_name);
		List<ExerciseStudyPlanInfo> list =preExerciseMapper.searchStudyPlanList(pa);
		for(ExerciseStudyPlanInfo studyInfo :list) {
			Map<String, String> m = new HashMap<String, String>();
			m.put("id",studyInfo.getId());
			List<DepartmentRespInfo> departmentList = preExerciseMapper.getDepartmentInfo(m);
			studyInfo.setPlan_department_id(departmentList);
		}
		return list;
	}

	@Override
	public int searchStudyPlanTotal(String plan_name, String plan_types, String study_time, String prepare_name) {
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("plan_name", plan_name);
		pa.put("plan_types", plan_types);
		pa.put("study_time", study_time);
		pa.put("prepare_name", prepare_name);
		return preExerciseMapper.searchStudyPlanTotal(pa);
	}

	@Override
	public void delStudyPlan(String id) {
		preExerciseMapper.delStudyPlan(id);
	}

	@Override
	public void updateStudyPlan(ExerciseStudyPlanReqInfo planInfo) {
		preExerciseMapper.updateStudyPlan(planInfo);
		
	}

	@Override
	public List<ExercisePlanInfo> searchPreExerciseListByApp(String plan_types, String time, int pageNumber,
			int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("plan_types", plan_types);
		pa.put("time", time);
		List<ExercisePlanInfo> list = preExerciseMapper.searchPreExerciseListByApp(pa);
		return list;
	}


	@Override
	public List<ExercisePlanDataRespInfo> searchPreExerciseDataList(String plan_name, String plan_types, String time,
			int pageNumber, int pageSize, String state) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("plan_name", plan_name);
		pa.put("plan_types", plan_types);
		pa.put("time", time);
		pa.put("state", state);
		return preExerciseMapper.searchPreExerciseDataList(pa);
	}


	@Override
	public ExercisePlanDataRespInfo searchdataDetails(String id) {
		ExercisePlanDataRespInfo list = preExerciseMapper.searchdataDetails(id);
		list.setSceneUrl(preExerciseMapper.searchSceneUrl(id));
		return list;
		
		
	}


	@Override
	public List<APPExerciseDataRespInfo> searchPlanDataByApp(int pageNumber, int pageSize, String user_id) {
		PageHelper.startPage(pageNumber, pageSize);
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("plan_types", plan_types);
//		map.put("time", time);
		return preExerciseMapper.searchPlanDataByApp();
	}

	//添加演练计划总结
	@Override
	public void appConclusInfo(APPExerciseConclusionReqInfo appConReqInfo) {
		preExerciseMapper.appConclusInfo(appConReqInfo);
	}

	//演练延迟
	@Override
	public void appPlanDelay(APPExerciseDelayReqInfo planDelay) {
		preExerciseMapper.appPlanDelay(planDelay);
	}

	//同意演练延迟
	@Override
	public void agreeDelay(String id) {
		preExerciseMapper.agreeDelay(id);
		
	}


	@Override
	public void notAgreeDelay(String id) {
		preExerciseMapper.notAgreeDelay(id);	
	}


	@Override
	public void performPlan(APPExerciseBasicReqInfo appConReqInfo) {
		List<Picture> piclist = new ArrayList<Picture>();
		preExerciseMapper.performPlan(appConReqInfo);
		if(appConReqInfo.getSceneUrl().size()>0) {
			for(Picture pic :appConReqInfo.getSceneUrl() ) {
				Picture pict =new Picture();
				pict.setPictureUrl(pic.getPictureUrl());
				pict.setPicturetype(pic.getPicturetype());
				pict.setId(appConReqInfo.getId());
				preExerciseMapper.insertsceneUrl(pict);
				piclist.add(pict);
			}
		}
		
	}

	/**
	 * app添加_执行演练_演练基本信息_New
	 */
    @Override
    public void performPlan_new(APPExerciseBasicReqInfo_wcc appConReqInfo)
    {
        // TODO 接口未写完
        //演练计划总结
        APPExerciseConclusionReqInfo appInfo = new APPExerciseConclusionReqInfo();
        appInfo.setId(appConReqInfo.getId());
        appInfo.setUser_id(appConReqInfo.getUser_id());
        appInfo.setModify_mea(appConReqInfo.getPlanToDo());
        appInfo.setExist_ques(appConReqInfo.getHadQuestion());
        preExerciseMapper.appConclusInfo(appInfo);
        
        if(appConReqInfo.getAnswers().size()>0)
        {
            for(DrillRecordCatalogInfo_App info : appConReqInfo.getAnswers())
            {
                for(DrillRecordExampleInfo_wcc exampleInfo : info.getExampleList()) 
                {
                    Map<String, String> pa = new HashMap<String, String>();
                    pa.put("example_id", exampleInfo.getId());
                    pa.put("user_id", appConReqInfo.getUser_id());
                    pa.put("plan_id", appConReqInfo.getId());
                    pa.put("check_id", exampleInfo.getChooseDetailsId());
                    preExerciseMapper.insertDrillRecord(pa);
                }
            }
        }
        //提交演练计划现场记录
        preExerciseMapper.performPlan_new(appConReqInfo);
        if(appConReqInfo.getImageArray().size()>0) {
            for(String pic :appConReqInfo.getImageArray() ) {
                Picture pict =new Picture();
                pict.setPictureUrl(pic);
                pict.setPicturetype("1");
                pict.setPre_id(appConReqInfo.getId());
                preExerciseMapper.insertsceneUrl(pict);
            }
        }
        
        if(appConReqInfo.getUserImageArray().size()>0) {
            for(String pic :appConReqInfo.getUserImageArray() ) {
                Picture pict =new Picture();
                pict.setPictureUrl(pic);
                pict.setPicturetype("2");
                pict.setPre_id(appConReqInfo.getId());
                preExerciseMapper.insertsceneUrl(pict);
            }
        }
    }
    
    

}
